import {searchClick} from '../events'
import { cacheTools } from 'nc-lightapp-front';
import requestApi from '../requestApi';
import buttonClick from "./buttonClick";

export default async function refreshButtonClick() {

    // 获取左侧树
    let treeData = await new Promise(resolve => {
        requestApi.getTreeData({
            data: {},
            success(data) {
                resolve(data);
            }
        });
    });
    // 左树数据打平
    let treenode = (treeData.data && treeData.data[1] || []).map(item => {
        item.pid = null;
        item.refname = item.refname || item.name;
        return item;
    });

    if (treenode && treenode.length === 0) return;

    let {syncTree} = this.props;
    // 备份当前的activeNode
    let currentTreeNode = syncTree.getSelectNode('tree1') && syncTree.getSelectNode('tree1').refpk || null;
    let {setSyncTreeData} = syncTree;
    let newTree = syncTree.createTreeData(treenode); //创建树 组件需要的数据结构
    setSyncTreeData('tree1', newTree);

    //请求分摊规则详情数据
    let reqData = {
        pagecode: this.props.getPageCode(),
        pk: currentTreeNode || treenode[0].refpk || treenode[0].refcode,
    };
    let tableData = await new Promise(resolve => {
        requestApi.getTableData({
            data: reqData,
            success: (data) => {
                resolve(data);
            }
        });
    });
    // 选中左树选中项
    syncTree.setNodeSelected('tree1', reqData.pk);

    buttonClick.setMetaDataFromTreeNode(tableData, this.props);
    this.pubMessage.refreshSuccess();
}
